package com.xianxing.oauth2.config;

import com.xianxing.oauth2.domain.User;
import com.xianxing.oauth2.service.UserService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //获取user信息
        User user = userService.findByUsername(s);
        //角色授权
        List<GrantedAuthority> grantedAuthorities=Lists.list();
        if(user!=null){
            //获取权限
            String userPower = user.getUserPower();
            //授权
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(userPower);
            grantedAuthorities.add(simpleGrantedAuthority);

        }
        return new org.springframework.security.core.userdetails.User(user.getUserName(),user.getUserPwd(),grantedAuthorities);
    }
}
